/**
 * 
 */
package com.nari.heatassist.action;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.json.JSONUtil;
import org.apache.struts2.json.annotations.JSON;

import com.nari.baseinfo.model.IBuilding;
import com.nari.baseinfo.model.ICommunity;
import com.nari.baseinfo.model.IHeatMeter;
import com.nari.baseinfo.model.IProject;
import com.nari.baseinfo.service.IBuildingService;
import com.nari.baseinfo.service.ICommunityService;
import com.nari.baseinfo.service.IHeatMeterService;
import com.nari.baseinfo.service.IProjectService;
import com.nari.coreframe.action.struts2.JSONActionSupport;
import com.nari.heatassist.model.IBuildingConsumeConfig;
import com.nari.heatassist.service.IBuildingConsumeConfigService;
import com.nari.heatassist.utils.BuildingConsumeConfigEntity;
import com.nari.platform.model.PageCond;
import com.nari.platform.model.ResultWithPageCond;
import com.opensymphony.xwork2.Action;

/**
 * 
 * @author Xiaopeng
 * @date 2014-4-22
 */
public class BuildingConsumeConfigAction extends JSONActionSupport {
	private static final long serialVersionUID = -8740186505541426137L;
	private IBuildingConsumeConfigService iBuildingConsumeConfigService;
	private IProjectService iProjectService;
	private ICommunityService iCommunityService;
	private IBuildingService iBuildingService;
	private IHeatMeterService iHeatMeterService;
	//楼栋耗热配置查询
	private String projectUID;
	private String communityUID;
	private String buildingUID;
	private List<IProject> projectList;
	private List<ICommunity> communityList;
	private List<IBuilding> buildingList;
	//分页查询
	private int pageIndex = 1; //默认查询第一页列表
	private PageCond pageCond;
	private int pageSize;
	private ResultWithPageCond<BuildingConsumeConfigEntity> consumeConfigPage;
	private Map<String, String> pageProjectMap; //所属项目id-projectName映射表
	private Map<String, String> pageCommunityMap; //所属小区id-communityName映射表
	private Map<String, String> pageBuildingMap; //所属楼栋id-buildingName映射表
	//楼栋关联的计量热表配置
	private ResultWithPageCond<IBuildingConsumeConfig> buildingMeasureMeterPage;
	private String meterNo;
	private ResultWithPageCond<IHeatMeter> meterPage;
	private String meterOptions;
	
	/**
	 * 楼栋热计量配置初始化页面
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-4-22
	 */
	public String initMeasureConfigMgr() throws Exception {
		//可设置携带的系统参数
		return "measureConfigMgr";
	}
	
	/**
	 * 分页查询楼栋热计量配置列表
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-4-22
	 */
	public String queryMeasureConfigPage() throws Exception {
		//所属项目列表
		this.projectList = this.iProjectService.queryAllProjectInOrder();
		if(this.projectList == null) {
			this.projectList = new ArrayList<IProject>();
		}
		//所属小区列表
		if(this.projectUID!=null && !"".equals(projectUID)) {
			this.communityList = this.iCommunityService.queryCommunitysByBelongings(null, null, null, projectUID);
		} else {
			this.communityList = new ArrayList<ICommunity>();
		}
		//所属楼栋列表
		this.buildingList = this.iBuildingService.queryBuildingByBelongings(projectUID, communityUID);
		//分页信息
		this.pageCond = new PageCond();
		int start = (this.pageIndex - 1) * this.pageCond.getLimit();
		this.pageCond.setStart(start);
		//分页结果
		this.consumeConfigPage = iBuildingConsumeConfigService.queryConsumeConfigPage(projectUID, communityUID, buildingUID, pageCond);
		//计算结果集的页数
		this.pageSize = new BigDecimal(this.consumeConfigPage.getPageCond().getTotal())
				.divide(new BigDecimal(this.consumeConfigPage.getPageCond().getLimit()), RoundingMode.UP).intValue();
		if(pageSize == 0) pageSize = 1;
		// 项目映射表
		this.pageProjectMap = new HashMap<String, String>();
		for (IProject project : this.projectList) {
			this.pageProjectMap.put(project.getId(), project.getProjectName());
		}
		// 小区映射表
		this.pageCommunityMap = iBuildingConsumeConfigService.queryReferCommunitysByConfigList(consumeConfigPage.getList());
		// 楼栋映射表
		this.pageBuildingMap = iBuildingConsumeConfigService.queryReferBuildingsByConfigList(consumeConfigPage.getList());
		return "measureConfigPage";
	}
	
	/**
	 * 删除楼栋热量计量配置
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-4-23
	 */
	public String deleteBuildingConsumeConfig() throws Exception {
		this.rmsg = iBuildingConsumeConfigService.deleteBuildingConsumeConfig(buildingUID);
		this.writeJsonStrToResponse(JSONUtil.serialize(rmsg));
		return Action.SUCCESS;
	}
	
	/**
	 * 查询楼栋热计量相关的热量表列表
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-4-23
	 */
	public String queryReferMeasureMeterPage() throws Exception {
		if(buildingUID!=null && !"".equals(buildingUID)) {
			//分页信息
			this.pageCond = new PageCond();
			int start = (this.pageIndex - 1) * this.pageCond.getLimit();
			this.pageCond.setStart(start);
			//计量热表分页查询
			this.buildingMeasureMeterPage = iBuildingConsumeConfigService.queryMeasureMeterPage(buildingUID, meterNo, pageCond);
			//计算结果集的页数
			this.pageSize = new BigDecimal(this.buildingMeasureMeterPage.getPageCond().getTotal())
					.divide(new BigDecimal(this.buildingMeasureMeterPage.getPageCond().getLimit()), RoundingMode.UP).intValue();
			if(pageSize == 0) pageSize = 1;
			//楼栋关系
			this.pageBuildingMap = iBuildingConsumeConfigService.queryReferBuildingsByMeasureConfigList(buildingMeasureMeterPage.getList());
		}
		return "referMeasureMeterPage";
	}
	
	/**
	 * 查询可用于楼栋耗热配置的热表列表
	 * 根据楼栋与热表的物理关系(主干、分支等关系)，只要热表与楼栋在同一小区，即可进行逻辑关系配置
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-4-24
	 */
	public String queryConfigMeasureMeterPage() throws Exception {
		IBuilding building = this.iBuildingService.queryEntityById(buildingUID);
		if(building != null) {
			this.projectUID = building.getProjectID();
			this.communityUID = building.getCommunityID();
			//分页信息
			this.pageCond = new PageCond();
			int start = (this.pageIndex - 1) * this.pageCond.getLimit();
			this.pageCond.setStart(start);
			//计量热表分页查询
			this.meterPage = iBuildingConsumeConfigService.queryConfigMeasureMeterPage(projectUID, communityUID, meterNo, buildingUID, pageCond);
			//计算结果集的页数
			this.pageSize = new BigDecimal(this.meterPage.getPageCond().getTotal())
					.divide(new BigDecimal(this.meterPage.getPageCond().getLimit()), RoundingMode.UP).intValue();
			if(pageSize == 0) pageSize = 1;
			//项目关系
			this.pageProjectMap = iHeatMeterService.queryReferProjectsByMeterList(meterPage.getList());
			//小区关系
			this.pageCommunityMap = iHeatMeterService.queryReferCommunitysByMeterList(meterPage.getList());
			//楼栋关系
			this.pageBuildingMap = iHeatMeterService.queryReferBuildingsByMeterList(meterPage.getList());
		}
		return "configMeasureMeterPage";
	}
	
	/**
	 * 保存楼栋耗热计量的热量表关系配置
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-4-24
	 */
	public String saveOrUpdateBuildingMeasureMeterConfig() throws Exception {
		this.rmsg = iBuildingConsumeConfigService.saveOrUpdateBuildingMeasureMeterConfig(buildingUID, meterOptions);
		this.writeJsonStrToResponse(JSONUtil.serialize(rmsg));
		return Action.SUCCESS;
	}
	
	/**
	 * 取消楼栋与热表的关联配置
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-4-24
	 */
	public String cancelBuildingConsumeConfig() throws Exception {
		this.rmsg = iBuildingConsumeConfigService.deleteConfigByRelation(buildingUID, meterNo);
		this.writeJsonStrToResponse(JSONUtil.serialize(rmsg));
		return Action.SUCCESS;
	}

	/**
	 * @return the iBuildingConsumeConfigService
	 */
	@JSON(serialize=false)
	public IBuildingConsumeConfigService getiBuildingConsumeConfigService() {
		return iBuildingConsumeConfigService;
	}

	/**
	 * @param iBuildingConsumeConfigService the iBuildingConsumeConfigService to set
	 */
	public void setiBuildingConsumeConfigService(
			IBuildingConsumeConfigService iBuildingConsumeConfigService) {
		this.iBuildingConsumeConfigService = iBuildingConsumeConfigService;
	}

	/**
	 * @return the iProjectService
	 */
	@JSON(serialize=false)
	public IProjectService getiProjectService() {
		return iProjectService;
	}

	/**
	 * @param iProjectService the iProjectService to set
	 */
	public void setiProjectService(IProjectService iProjectService) {
		this.iProjectService = iProjectService;
	}

	/**
	 * @return the iCommunityService
	 */
	@JSON(serialize=false)
	public ICommunityService getiCommunityService() {
		return iCommunityService;
	}

	/**
	 * @param iCommunityService the iCommunityService to set
	 */
	public void setiCommunityService(ICommunityService iCommunityService) {
		this.iCommunityService = iCommunityService;
	}

	/**
	 * @return the iBuildingService
	 */
	@JSON(serialize=false)
	public IBuildingService getiBuildingService() {
		return iBuildingService;
	}

	/**
	 * @param iBuildingService the iBuildingService to set
	 */
	public void setiBuildingService(IBuildingService iBuildingService) {
		this.iBuildingService = iBuildingService;
	}

	/**
	 * @return the iHeatMeterService
	 */
	@JSON(serialize=false)
	public IHeatMeterService getiHeatMeterService() {
		return iHeatMeterService;
	}

	/**
	 * @param iHeatMeterService the iHeatMeterService to set
	 */
	public void setiHeatMeterService(IHeatMeterService iHeatMeterService) {
		this.iHeatMeterService = iHeatMeterService;
	}

	/**
	 * @return the projectUID
	 */
	public String getProjectUID() {
		return projectUID;
	}

	/**
	 * @param projectUID the projectUID to set
	 */
	public void setProjectUID(String projectUID) {
		this.projectUID = projectUID;
	}

	/**
	 * @return the communityUID
	 */
	public String getCommunityUID() {
		return communityUID;
	}

	/**
	 * @param communityUID the communityUID to set
	 */
	public void setCommunityUID(String communityUID) {
		this.communityUID = communityUID;
	}

	/**
	 * @return the buildingUID
	 */
	public String getBuildingUID() {
		return buildingUID;
	}

	/**
	 * @param buildingUID the buildingUID to set
	 */
	public void setBuildingUID(String buildingUID) {
		this.buildingUID = buildingUID;
	}

	/**
	 * @return the projectList
	 */
	public List<IProject> getProjectList() {
		return projectList;
	}

	/**
	 * @param projectList the projectList to set
	 */
	public void setProjectList(List<IProject> projectList) {
		this.projectList = projectList;
	}

	/**
	 * @return the communityList
	 */
	public List<ICommunity> getCommunityList() {
		return communityList;
	}

	/**
	 * @param communityList the communityList to set
	 */
	public void setCommunityList(List<ICommunity> communityList) {
		this.communityList = communityList;
	}

	/**
	 * @return the buildingList
	 */
	public List<IBuilding> getBuildingList() {
		return buildingList;
	}

	/**
	 * @param buildingList the buildingList to set
	 */
	public void setBuildingList(List<IBuilding> buildingList) {
		this.buildingList = buildingList;
	}

	/**
	 * @return the pageIndex
	 */
	public int getPageIndex() {
		return pageIndex;
	}

	/**
	 * @param pageIndex the pageIndex to set
	 */
	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	/**
	 * @return the pageCond
	 */
	public PageCond getPageCond() {
		return pageCond;
	}

	/**
	 * @param pageCond the pageCond to set
	 */
	public void setPageCond(PageCond pageCond) {
		this.pageCond = pageCond;
	}

	/**
	 * @return the pageSize
	 */
	public int getPageSize() {
		return pageSize;
	}

	/**
	 * @param pageSize the pageSize to set
	 */
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	/**
	 * @return the consumeConfigPage
	 */
	public ResultWithPageCond<BuildingConsumeConfigEntity> getConsumeConfigPage() {
		return consumeConfigPage;
	}

	/**
	 * @param consumeConfigPage the consumeConfigPage to set
	 */
	public void setConsumeConfigPage(
			ResultWithPageCond<BuildingConsumeConfigEntity> consumeConfigPage) {
		this.consumeConfigPage = consumeConfigPage;
	}

	/**
	 * @return the pageProjectMap
	 */
	public Map<String, String> getPageProjectMap() {
		return pageProjectMap;
	}

	/**
	 * @param pageProjectMap the pageProjectMap to set
	 */
	public void setPageProjectMap(Map<String, String> pageProjectMap) {
		this.pageProjectMap = pageProjectMap;
	}

	/**
	 * @return the pageCommunityMap
	 */
	public Map<String, String> getPageCommunityMap() {
		return pageCommunityMap;
	}

	/**
	 * @param pageCommunityMap the pageCommunityMap to set
	 */
	public void setPageCommunityMap(Map<String, String> pageCommunityMap) {
		this.pageCommunityMap = pageCommunityMap;
	}

	/**
	 * @return the pageBuildingMap
	 */
	public Map<String, String> getPageBuildingMap() {
		return pageBuildingMap;
	}

	/**
	 * @param pageBuildingMap the pageBuildingMap to set
	 */
	public void setPageBuildingMap(Map<String, String> pageBuildingMap) {
		this.pageBuildingMap = pageBuildingMap;
	}

	/**
	 * @return the buildingMeasureMeterPage
	 */
	public ResultWithPageCond<IBuildingConsumeConfig> getBuildingMeasureMeterPage() {
		return buildingMeasureMeterPage;
	}

	/**
	 * @param buildingMeasureMeterPage the buildingMeasureMeterPage to set
	 */
	public void setBuildingMeasureMeterPage(
			ResultWithPageCond<IBuildingConsumeConfig> buildingMeasureMeterPage) {
		this.buildingMeasureMeterPage = buildingMeasureMeterPage;
	}

	/**
	 * @return the meterNo
	 */
	public String getMeterNo() {
		return meterNo;
	}

	/**
	 * @param meterNo the meterNo to set
	 */
	public void setMeterNo(String meterNo) {
		this.meterNo = meterNo;
	}

	/**
	 * @return the meterPage
	 */
	public ResultWithPageCond<IHeatMeter> getMeterPage() {
		return meterPage;
	}

	/**
	 * @param meterPage the meterPage to set
	 */
	public void setMeterPage(ResultWithPageCond<IHeatMeter> meterPage) {
		this.meterPage = meterPage;
	}

	/**
	 * @return the meterOptions
	 */
	public String getMeterOptions() {
		return meterOptions;
	}

	/**
	 * @param meterOptions the meterOptions to set
	 */
	public void setMeterOptions(String meterOptions) {
		this.meterOptions = meterOptions;
	}
	
}
